package com.dongdongqiang.daogenerator.tablemetabuilder;

import com.dongdongqiang.daogenerator.pojo.TableColumn;
import com.dongdongqiang.daogenerator.utils.Misc;

import javax.persistence.Column;
import java.lang.reflect.Field;

/**
 * Created by ZZB on 2017/12/3.
 */
public class TableColumnBuilderImpl implements TableColumnBuilder {
    private EntityToEntityMetaInfoConvertor convertor;

    public TableColumnBuilderImpl(EntityToEntityMetaInfoConvertor convertor) {
        this.convertor = convertor;
    }

    public TableColumn obtainByField(Field declaredField){
        TableColumn tableColumn = new TableColumn();
        Column columnAnnotation = declaredField.getAnnotation(Column.class);
        String columnName;
        if(columnAnnotation != null){
            columnName = columnAnnotation.name();
        } else {
            columnName = Misc.camelToUnderline(declaredField.getName());
        }
        tableColumn.setColumnName(columnName);
        tableColumn.setPropertyName(declaredField.getName());
        tableColumn.setPropertyType(declaredField.getType());
        convertor.optimizeTableColumn(tableColumn,declaredField);
        return tableColumn;
    }

}
